package com.healthdata.service.impl;

import com.healthdata.service.CalculateService;
import com.healthdata.service.ComparisonService;
import com.healthdata.vo.HorizonVO;
import org.apache.commons.math3.stat.inference.TTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


//对比分析：多组数据的横向对比与差异分析
@Service
public class ComparisonServiceImpl implements ComparisonService {

    @Autowired
    private CalculateService calculateService;

    public HorizonVO horizontalComparison(double[] data1,double[] data2){

        HorizonVO horizonVO=new HorizonVO();

        double mean1=calculateService.mean(data1);
        double mean2=calculateService.mean(data2);
        double deviation1=calculateService.deviation(data1);
        double deviation2=calculateService.deviation(data2);
        if(mean1<mean2){
            horizonVO.setMeanResult("第二组平均水平更高");
        }
        if(mean1>mean2){
            horizonVO.setMeanResult("第一组平均水平更高");
        }
        if(mean1==mean2){
            horizonVO.setMeanResult("两组平均水平一致");
        }
        if(deviation1<deviation2){
            horizonVO.setChangeResult("第二组水平不平稳");
        }
        if(deviation1>deviation2){
            horizonVO.setChangeResult("第一组水平不平稳");
        }
        if(deviation1==deviation2){
            horizonVO.setChangeResult("二者波动性一致");
        }

        return horizonVO;
    }


    //差异性检验，t检验
    public String tTest(double[] data1,double[] data2){
        // 创建TTest对象
        TTest tTest = new TTest();

        // 进行独立样本t检验
        double pValue = tTest.tTest(data1, data2);

        // 输出 p 值
        System.out.println("p-value: " + pValue);

        // 判断显著性
        if (pValue < 0.05) {
            return ("拒绝零假设，二者有显著差异。");
        } else {
            return ("接受零假设，二者没有显著差异。");
        }
    }
}
